//
//  IAStudentTableCell.m
//  VoiceClass
//
//  Created by caoxiaobo on 2018/1/11.
//  Copyright © 2018年 YRX. All rights reserved.
//

#import "IAStudentTableCell.h"
#import "Definition.h"
#import "UIView+Extension.h"
#import "IAStudent.h"

static CGFloat const qLRPadding = 12.0f;

static CGFloat const qInset = 2.0f;

@interface IAStudentTableCell ()

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idLabel;
@property (nonatomic, strong) UILabel *gradeLabel;

@end

@implementation IAStudentTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _iconView = [[UIImageView alloc] init];
        _iconView.layer.cornerRadius = 4.0f;
        _iconView.clipsToBounds = YES;
        _iconView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _iconView.layer.borderWidth = 1.0f;
        
        _nameLabel = [self assembleLabel:18.0f textColor:[UIColor darkGrayColor] numLines:1];
        _idLabel = [self assembleLabel:16.0f textColor:HEXCOLOR(0x999999) numLines:1];
        _gradeLabel = [self assembleLabel:16.0f textColor:HEXCOLOR(0x999999) numLines:1];
        
        [self.contentView addSubview:_iconView];
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_idLabel];
        [self.contentView addSubview:_gradeLabel];
    }
    return self;
}

- (UILabel *)assembleLabel:(CGFloat)fontSize textColor:(UIColor *)tColor numLines:(NSInteger)numLines {
    UILabel *aLabel = [UILabel new];
    aLabel.textColor = tColor;
    aLabel.numberOfLines = numLines;
    aLabel.font = [UIFont systemFontOfSize:fontSize];
    return aLabel;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat const iconWH = 38.0;
    self.iconView.frame = CGRectMake(qLRPadding + 4, qLRPadding, iconWH, iconWH);
    
    CGFloat nameH = 24;
    CGFloat nameX = CGRectGetMaxX(self.iconView.frame) + qLRPadding;
    CGFloat nameY = (iconWH - nameH) * 0.5 + qLRPadding;
    CGFloat nameW = self.contentView.cWidth - nameX - qLRPadding;
    self.nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
    
    CGFloat subLabelH = 22.0f;
    CGFloat idLabelX = nameX;
    CGFloat idLabelY = CGRectGetMaxY(self.iconView.frame) + qInset;
    CGFloat idLabelW = nameW;
    self.idLabel.frame = CGRectMake(idLabelX, idLabelY, idLabelW, subLabelH);
    
    CGFloat gradeLabelH = CGRectGetMaxY(self.idLabel.frame) + qInset;
    self.gradeLabel.frame = CGRectMake(idLabelX, idLabelY, idLabelW, gradeLabelH);
    
    // 12 + 38 +  22 + 22 + 4 + 12 = 110
    
}

- (void)setStudent:(IAStudent *)student {
    _student = student;
    
    self.iconView.image = [UIImage imageNamed:student.avatar];
    self.nameLabel.text = [NSString stringWithFormat:@"姓名：%@", student.name];
    self.idLabel.text = [NSString stringWithFormat:@"学号：%@", student.stuID];
    self.gradeLabel.text = [NSString stringWithFormat:@"班级：%@", student.grade];
}

@end
